% This script performs some Risk Portfolio Analysis.
% For more details go to "www.gerardoferrara.com", by G. Ferrara.

clc; clear; close all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[DATABASE LABELS NNN] =xlsread('database.xls','Series','B1:I17')

AV_RET=mean(DATABASE)

figure(1)
barh(AV_RET)
set(gca,'YTickLabel',LABELS)
title('Average Annual Returns')

ST_DEV=std(DATABASE)


figure(2)
barh(ST_DEV)
set(gca,'YTickLabel',LABELS)
title('Sigma of Annual Returns')


figure(3)
subplot(2,1,1)
barh(AV_RET)
set(gca,'YTickLabel',LABELS)
title('Average Annual Returns')

subplot(2,1,2)
barh(ST_DEV)
set(gca,'YTickLabel',LABELS)
title('Sigma of Annual Returns')

figure(4)
scatter(ST_DEV, AV_RET)
title('RISK-AVERAGE RETURN COMBINATION')
xlabel('Sigma')
ylabel('Average Return')


EWP=0.125*ones(8,1);


figure(5)
pie(EWP)
title('Equally Weighted Portfolio')
legend(LABELS, 'Location','NorthEastOutside')


AV_RET_PORT=AV_RET*EWP;

CORR=corr(DATABASE)


SIGMA_WEIGHTS=EWP'.*ST_DEV;

ST_DEV_PORT=sqrt(SIGMA_WEIGHTS*CORR*SIGMA_WEIGHTS');

figure(6)
scatter(ST_DEV, AV_RET)
hold on
scatter(ST_DEV_PORT, AV_RET_PORT)
hold off
title('RISK-AVERAGE RETURN COMBINATION')
xlabel('Sigma')
ylabel('Average Return')


VAR_95=AV_RET-1.645*ST_DEV

VAR_99=AV_RET-2.326*ST_DEV


figure(7)
barh([VAR_95' VAR_99'])
legend({'95%';'99%'}, 'Location','NorthEastOutside')
set(gca,'YTickLabel',LABELS)
title('VaR 95% versus VaR 99%')